<?php
namespace app\common\model;

use think\model\Merge;
use think\Request;

class CmsPictureModel extends Merge
{
    protected $name = 'cms_picture'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    //类型自动转换
    protected $type = [
        'atlas' => 'json',
    ];
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = ['uid']; //插入
    protected $update = []; //修改
    // 定义关联模型列表
    protected $relationModel = ['CmsPictureData'];
    // 定义关联外键
    protected $fk = 'aid';
    // 为混淆字段定义映射
    protected $mapFields = [
        'aid'      => 'cms_picture.aid',
        'data_aid' => 'CmsPictureData.aid',
    ];

    /*读取器*/
    protected function getStatusTextAttr($value, $data)
    {
        $status = [
            0 => '<span class="label label-warning">待审</span>',
            1 => '<span class="label label-success">已审</span>',
        ];
        return $status[$data['status']];
    }

    protected function getContentAttr($value)
    {
        return stripslashes(htmlspecialchars_decode($value));
    }

    //图集还原数组
    protected function getAtlasArrAttr($value, $data)
    {
        $list      = explode('###', $data['atlas']);
        $imgList   = explode('|', $list[0]); //图片
        $titleList = explode('|', $list[1]); //标题
        $infoList  = explode('|', $list[2]); //描述
        foreach ($imgList as $k => $v) {
            $arr[$k]['src']   = $v;
            $arr[$k]['title'] = $titleList[$k];
            $arr[$k]['info']  = $infoList[$k];
        }
        return $arr;
    }

    /*修改器*/
    protected function setDescriptionAttr($value, $data)
    {
        if (empty($value)) {
            $str = del_html(html_out($data['content']));
            if (empty($str)) {
                return $data['title'];
            }
            return substr($str, 0, 240);
        }
        return $value;
    }

    protected function setFlagAttr($value, $data)
    {
        if (empty($value)) {
            return '';
        } else {
            return implode(',', $value);
        }
    }

}